import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  Patch,
  Query,
  ClassSerializerInterceptor,
  UseInterceptors,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBody,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { StudentService } from './student.service';
import { CreateStudentDto } from './dto/create-student.dto';
import { UpdateStudentDto } from './dto/update-student.dto';
import { ReturnStudentDtoList, GetStudentDto } from './dto/get-student.dto';
import { JwtAuthGuardsGuard } from '../guards/jwt-auth.guards.guard';
import { JwtGuard } from '../guards/jwt.guards';
import { CreatedStudentDtoPipePipe } from './pipe/created-student.dto.pipe';

@Controller('student')
@UseGuards(JwtGuard) // 校验token是否通过
@UseInterceptors(ClassSerializerInterceptor)
@ApiTags('学生模块')
export class UserController {
  constructor(private readonly StudentService: StudentService) {}

  @Post()
  @ApiOperation({ summary: '创建学生' })
  @ApiBody({ type: CreateStudentDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateStudentDto,
  })
  create(@Body(CreatedStudentDtoPipePipe) CreateStudentDto: CreateStudentDto) {
    return this.StudentService.create(CreateStudentDto);
  }

  @ApiOperation({ summary: '学生列表' })
  @ApiQuery({ type: GetStudentDto, description: '参数可选' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: ReturnStudentDtoList,
  })
  @Get()
  @UseGuards(JwtAuthGuardsGuard) // 根据用户处理系信息
  findAll(@Query() query: GetStudentDto) {
    return this.StudentService.findAll(query);
  }

  @ApiOperation({ summary: '学生详情' })
  @ApiQuery({ name: 'id', required: true, description: '学生id' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateStudentDto,
  })
  @Get(':id')
  findUserDetail(@Param('id') id: string) {
    return this.StudentService.findUserDetail(id);
  }

  @ApiOperation({ summary: '学生更新' })
  @ApiQuery({ name: 'id', required: true, description: '学生id' })
  @ApiBody({ type: CreateStudentDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateStudentDto,
  })
  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateStudentDto) {
    return this.StudentService.update(id, updateUserDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '学生删除' })
  @ApiQuery({ name: 'id', required: true, description: '学生id' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreateStudentDto,
  })
  remove(@Param('id') id: string) {
    return this.StudentService.remove(id);
  }
}
